package com.example.HelloSpace.factory;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.example.HelloSpace.repository.MissionRepository;
import com.example.HelloSpace.viewmodels.MissionListViewModel;

public class MissionListViewModelFactory implements ViewModelProvider.Factory {
    private final MissionRepository repository;

    // 构造函数中接收依赖项
    public MissionListViewModelFactory(MissionRepository repository) {
        this.repository = repository;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        if (modelClass.isAssignableFrom(MissionListViewModel.class)) {
            //noinspection unchecked
            return (T) new MissionListViewModel(repository);
        }
        throw new IllegalArgumentException("Unknown ViewModel class");
    }
}